<template>
  <div class="topbar">
    <!-- 版心 -->
    <div class="wrap topbarwrap">
      <!-- 左侧 -->
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <!-- 右侧导航 -->
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" width="23px" alt="" />
          用户名:{{userInfo.nickName}}
        </li>
        <li>我的积分:{{userInfo.coin}}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{cartTotal}}</strong>
        </li>
        <li class="btnlogin" @click="changeIsShowLoginModal(true)" v-else>  登录 </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from "vuex";
import { WeiXinLoginAPI } from "../request/api.js";
export default {
  async created() {
    console.log("topbar-created...", this.$route.query.code);
    let mycode = this.$route.query.code;
    if (mycode) {
      let res = await WeiXinLoginAPI({
        code: mycode,
      });
      if (res.code == 0) {
        // 1.提示
        alert("扫码登录成功");
        // 2.存token
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        // 清除code
        this.$router.push(this.$route.path);
        // 更新登录状态
        this.changeIsLogined(true);
        // 更新用户信息
        this.asyncChangeUserInfo();
      } else if (res.code == 400) {
        // 二维码失效 需要重新扫码
        alert("请重新扫码");
        // 2.帮用户打开登录窗口
        this.changeIsShowLoginModal(true);
      } else if (res.code == 407) {
        alert("请使用手机号再登录一次");
        this.changeIsShowLoginModal(true);
        localStorage.setItem("uuid", res.uuid);
      } 
    }else {
        console.log("没有通过扫码就进来了");
        let mytoken = localStorage.getItem("x-auth-token")
        this.changeIsLogined(Boolean(mytoken));
        // 如果当前未登录，用户信息应该要初始化
        if (mytoken) {
          // 登录成功后，刷新页面新用户信息没有了
          this.asyncChangeUserInfo()
        } else {
          console.log("初始化");
          this.initUserInfo()
        }
      }
  },
  watch:{
    // // 监听地址变化
    // "$route.path":{
    //   handler(newVal,oldVal){
    //     console.log("地址变化了",newVal,oldVal);
    //     let mytoken = localStorage.getItem("x-auth-token");
    //     this .changeIsLogined(Boolean(mytoken))
    //   }
    // }
  },
  computed: {
    ...mapState({
      isLogined: state => state.loginStatus.isLogined,
      cartTotal: state => state.userInfo.cartTotal,
      userInfo: state => state.userInfo.userInfo,
    }),
  },
  methods: {
    ...mapMutations({
      //当前组件的方法名："模块名/模块下的方法名"
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsLogined: "loginStatus/changeIsLogined",
      initUserInfo:"userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncChangeUserInfo:"userInfo/asyncChangeUserInfo"
    })
  },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background-color: #242b39;
  font-size: 14px;
  color: #fffefe;
  .topbarwrap {
    display: flex;
    justify-content: space-between;
  }
  .right {
    li {
      float: left;
      margin-left: 20px;
      cursor: pointer;
      text-align: center;
      img {
        vertical-align: middle;
        border-radius: 50%;
      }
      &.btnlogin {
        width: 124px;
        height: 40px;
        background: #0a328e;
      }
    }
  }
}
.cart-btn {
  width: 124px;
  height: 40px;
  background: #0a328e;
  text-align: center;
  img {
    width: 20px;
    height: 19px;
    vertical-align: middle;
  }
  span {
    margin: 0 8px;
  }
  strong {
    width: 22px;
    height: 22px;
    line-height: 22px;
    display: inline-block;
    border-radius: 50%;
    background-color: #fd604d;
  }
}
</style>
